﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class webcon_dateselector : System.Web.UI.UserControl
{
    private DateTime _date;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public int BeginYear
    {
        set
        {
            this.hidValues.Attributes.Add("BeginYear", value.ToString());
        }
        get
        {
            if (this.hidValues.Attributes["BeginYear"] == null)
            {
                return 0;
            }
            return Convert.ToInt32(this.hidValues.Attributes["BeginYear"]);
        }
    }

    public int EndYear
    {
        set
        {
            this.hidValues.Attributes.Add("EndYear", value.ToString());
        }
        get
        {
            if (this.hidValues.Attributes["EndYear"] == null)
            {
                return 0;
            }
            return Convert.ToInt32(this.hidValues.Attributes["EndYear"]);
        }
    }

    public bool PostBack
    {
        set
        {
            this.aspPanel.Visible = value;
            this.htmlPanel.Visible = !value;
        }
        get
        {
            return aspPanel.Visible;
        }
    }

    public string selYearID
    {
        get
        {
            return this.selYear.ClientID;
        }
    }

    public string selMonthID
    {
        get
        {
            return this.selMonth.ClientID;
        }
    }

    public string selDayID
    {
        get
        {
            return this.selDay.ClientID;
        }
    }

    public DateTime Date
    {
        set
        {
            this._date = value;
            if (this.PostBack)
            {
                this.BindDate(value);
            }
        }
        get
        {
            string d = "";
            try
            {
                if (!this.PostBack)
                {
                    d = Request.Params[this.selMonth.Name] + "/" + Request.Params[this.selDay.Name] + "/" + Request.Params[this.selYear.Name] + " 00:00:00";
                    this._date = DateTime.Parse(d);
                }
                else
                {
                    d = this.ddMonth.SelectedValue + "/" + this.ddDay.SelectedValue + "/" + this.ddYear.SelectedValue + " 00:00:00";
                    this._date = DateTime.Parse(d);
                }
            }
            catch
            {
                return DateTime.Now;
            }
            return this._date;
        }
    }

    public string Y
    {
        get
        {
            return this._date.Year.ToString();
        }
    }

    public string M
    {
        get
        {
            return this._date.Month.ToString();
        }
    }

    public string D
    {
        get
        {
            return this._date.Day.ToString();
        }
    }

    public bool ReadOnly
    {
        set
        {
            this.selDay.Disabled = value;
            this.selMonth.Disabled = value;
            this.selYear.Disabled = value;
            this.ddYear.Enabled = !value;
            this.ddMonth.Enabled = !value;
            this.ddDay.Enabled = !value;
        }
        get
        {
            if (this.PostBack)
            {
                return !this.ddDay.Enabled;
            }
            else
            {
                return this.selDay.Disabled && this.selMonth.Disabled && this.selYear.Disabled;
            }
        }
    }

    private void BindDate(DateTime d)
    {
        this.ddYear.Items.Clear();
        int by = this.BeginYear == 0 ? DateTime.Now.Year - 10 : this.BeginYear;
        int ey = this.EndYear == 0 ? DateTime.Now.Year + 10 : this.EndYear;
        for (int i = by; i < ey; i++)
        {
            ListItem li = new ListItem(i.ToString(), i.ToString());
            this.ddYear.Items.Add(li);
        }
        this.ddDay.Items.Clear();
        for (int i = 0; i <= 31; i++)
        {
            int mm = d.Month;
            if (i == 29 && mm == 2 && (this._date.Year % 4) != 0)
            {
                break;
            }
            if (i == 30 && mm == 2 && (this._date.Year % 4) == 0)
            {
                break;
            }
            if (i == 31 && mm != 1 && mm != 3 && mm != 5 && mm != 7 && mm != 8 && mm != 10 && mm != 12)
            {
                break;
            }
            ListItem li = new ListItem(i.ToString(), i.ToString());
            this.ddDay.Items.Add(li);
        }
        this.ddDay.SelectedValue = d.Day.ToString();
        this.ddYear.SelectedValue = d.Year.ToString();
        this.ddMonth.SelectedValue = d.Month.ToString();
    }
    protected void ddYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        string d = "1/1/" + this.ddYear.SelectedValue + " 00:00:00";
        this.BindDate(DateTime.Parse(d));
    }
    protected void ddMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        string d = this.ddMonth.SelectedValue + "/" + "1/" + this.ddYear.SelectedValue + " 00:00:00";
        this.BindDate(DateTime.Parse(d));
    }
}
